<template>
  <a-modal
    title="编辑用户信息"
    width="45%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="姓名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入姓名"
            v-decorator="['name', {
              initialValue: user.name,
              rules: [{required: true, min: 1, max: 10, message: '字符长度大于1小于10'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="手机号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入手机号码"
            v-decorator="['phone', {
              initialValue: user.phone,
              rules: [{required: true, pattern: regs.PHONE_REG, message: '请输入正确的手机号码格式'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="身份证号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入身份证号"
            v-decorator="['idcardNum', {
              initialValue: user.idcardNum,
              rules: [{required: true, validator: handleIdCardNumVarify, message: '请输入正确的身份证号格式'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="开户行"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入开户行"
            v-decorator="['bankName', {
              initialValue: user.bankName,
              rules: [{required: true, message: '请输入正确的开户行'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="银行卡号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入银行卡号"
            v-decorator="['bankCardNum', {
              initialValue: user.bankCardNum,
              rules: [{required: true, pattern: regs.BANK_CARD_NUM_REG, message: '请输入正确的银行卡号格式'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="服务企业"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            v-decorator="['enterpriseId', {
              initialValue: user.enterpriseUserId,
              rules: [{ required: true, type: 'number', message: '请选择服务企业' }] },
            ]"
            placeholder="请选择服务企业"
            :options="enterpriseSelects"
          />
        </a-form-item>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
import { updateUser, getEnterpriseOptions } from '@/api/manage'
import regs from '@/constants/pattern'

export default {
  data() {
    return {
      regs,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      form: this.$form.createForm(this),
      visible: false,
      confirmLoading: false,
      user: {
        bankCardNum: '',
        bankName: '',
        enterpriseUserId: '',
        enterpriseUserName: '',
        idcardNum: '',
        name: '',
        phone: ''
      },
      enterpriseOptions: []
    }
  },
  computed: {
    enterpriseSelects() {
      return this.enterpriseOptions.map(({ enterpriseId, enterpriseName }) => ({
        value: enterpriseId,
        label: enterpriseName
      }))
    }
  },
  methods: {
    handleIdCardNumVarify(rule, value, callback) {
      if (value.length === 15 && regs.ID_CARD_NUM_REG_15.test(value)) {
        callback()
      } else if (value.length === 18 && regs.ID_CARD_NUM_REG_18.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的身份证号格式'))
      }
    },
    handleSubmit() {
      this.confirmLoading = true
      this.form.validateFields((errors, values) => {
        if (!errors) {
          updateUser(this.user.individualUserId, values)
            .then(() => {
              this.handleCancel()
              this.$emit('onSuccess')
            })
            .finally(() => { this.confirmLoading = false })
        } else {
          this.confirmLoading = false
        }
      })
    },
    show(user) {
      this.visible = true
      this.user = user
      getEnterpriseOptions()
        .then(res => {
          this.enterpriseOptions = res.resData
        })
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>
